﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.AdvTree;
using DevComponents.DotNetBar;
using PPI.Client.Utils;
using PPI.Entities.XML;
using VMind.Global;
using ColumnHeader=DevComponents.AdvTree.ColumnHeader;

namespace PPI.Controls.Grid
{
    public partial class TreeGridBase : UserControl, IGrid
    {
        protected ElementStyle m_ChildElementStyle;
        protected EXmlOutboxBase m_MobileBase;
        protected BackgroundWorker m_LoadDataWorker;
        public EXmlOutboxBase MobileBase
        {
            get { return m_MobileBase; }
            set { m_MobileBase = value; }
        }

        public TreeGridBase()
        {
            InitializeComponent();
            m_ChildElementStyle = new ElementStyle();
            m_LoadDataWorker = new BackgroundWorker();
            m_LoadDataWorker.DoWork += new DoWorkEventHandler(m_LoadDataWorker_DoWork);
        }

        void m_LoadDataWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            LoadTree();
        }

        protected virtual void LoadTreeAsync()
        {
            m_LoadDataWorker.RunWorkerAsync();
        }

        protected virtual void ShowMenuOptions()
        {
            Node selectedNode = treeGrid.SelectedNode;
            if (selectedNode == null)
            {
                cmiAction.Enabled = false;
                return;
            }else
            {
                cmiAction.Enabled = true;
                if (selectedNode.Level > 0)
                    selectedNode = selectedNode.Parent;
                Int32 idUtility = ConvertHelper.ToInt32(selectedNode.Cells[1].Text);
                if (idUtility == Int32.MinValue && GetParentId(selectedNode) != Int32.MinValue)
                {
                    cmiAssociateUtility.Visible = true;
                    cmiCreateAsset.Visible = true;
                }
                else
                {
                    cmiAssociateUtility.Visible = false;
                    cmiCreateAsset.Visible = false;
                }
                
            }
        }
        protected virtual bool ShowMenu()
        {
            return true;
        }
        protected virtual void LoadTree()
        {

        }
        protected virtual bool ActivateDoubleClick()
        {
            return true;
        }
        protected virtual bool CancelActionChangeParent(Node currentNode, Node oldParentNode, Node newParentNode)
        {
            return false;
        }
        protected virtual void BeforeNodeChangeParent(Node currentNode, Node oldParentNode, Node newParentNode)
        {

        }
        public virtual string ExportToExcel(FileInfo savedFile)
        {
            ExportToExcelHelper helperExcel = new ExportToExcelHelper(savedFile);
            helperExcel.AddRow();
            foreach (ColumnHeader h in treeGrid.Columns)
            {
                helperExcel.WriteHeader(h.Text);   
            }
            foreach (Node n in treeGrid.Nodes)
            {
                helperExcel.AddRow();
                foreach (Cell c in n.Cells)
                {
                    helperExcel.WriteCell(c.Text);
                }
                if(n.HasChildNodes)
                {
                    helperExcel.AddRow();
                    foreach (ColumnHeader columnHeaderChild in n.NodesColumns)
                    {
                        helperExcel.WriteHeaderChild(columnHeaderChild.Text);
                    }
                    foreach (Node childNode in n.Nodes)
                    {
                        helperExcel.AddRow();
                        foreach (Cell childCell in childNode.Cells)
                        {
                            helperExcel.WriteCellChild(childCell.Text);
                        }
                    }
                    
                }
            }
            helperExcel.Close();
            return savedFile.FullName;
        }

        protected virtual Int32 GetParentId(Node node)
        {
            Int32 returnValue = Int32.MinValue;
            if(node.Parent != null)
            {
                returnValue = ConvertHelper.ToInt32(node.Parent.Text); 
            }else
            {
                returnValue = ConvertHelper.ToInt32(node.Text); 
            }
            return returnValue;
        }
        protected virtual Int32 GetChildId(Node node)
        {
            Int32 returnValue = Int32.MinValue;
            try
            {
                if (node.Level > 0)
                {
                    returnValue = ConvertHelper.ToInt32(node.Text);
                }
            }
            catch  { }
            return returnValue;
        }
        public void RemoveNode(Node node)
        {
            treeGrid.Nodes.Remove(node);
        }

        #region Events

        #region Owner
        public event EventHandler<TreeGridBaseEventArgs> ClickedEdit;
        public event EventHandler<TreeGridBaseEventArgs> ClickedEditWithoutImages;
        public event EventHandler<TreeGridBaseEventArgs> ClickedDelete;
        public event EventHandler<TreeGridBaseEventArgs> ClickedAssociate;
        public event EventHandler<TreeGridBaseEventArgs> ClickedCreateAsset;
        public event EventHandler ClickedExportToExcel;
        private void OnClickedEdit(object o, TreeGridBaseEventArgs e)
        {
            // Safely invoke an event:
            if (ClickedEdit != null)
            {
                ClickedEdit(o, e);
            }
        }
        private void OnClickedEditWithoutImages(object o, TreeGridBaseEventArgs e)
        {
            // Safely invoke an event:
            if (ClickedEditWithoutImages != null)
            {
                ClickedEditWithoutImages(o, e);
            }
        }
        private void OnClickedDelete(object o, TreeGridBaseEventArgs e)
        {
            // Safely invoke an event:
            if (ClickedDelete != null)
            {
                ClickedDelete(o, e);
            }
        }
        private void OnClickedAssociate(object o, TreeGridBaseEventArgs e)
        {
            // Safely invoke an event:
            if (ClickedAssociate != null)
            {
                ClickedAssociate(o, e);
            }
        }
        private void OnClickedCreateAsset(object o, TreeGridBaseEventArgs e)
        {
            // Safely invoke an event:
            if (ClickedCreateAsset != null)
            {
                ClickedCreateAsset(o, e);
            }
        }
        private void OnClickedExportToExcel(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (ClickedExportToExcel != null)
            {
                ClickedExportToExcel(o, e);
            }
        }


        #endregion

        #region Menu


        private void cmbMenu_PopupOpen(object sender, DevComponents.DotNetBar.PopupOpenEventArgs e)
        {
            ShowMenuOptions();
            e.Cancel = ShowMenu();

        }
        private void cmiEditWhithOutImages_Click(object sender, EventArgs e)
        {
            OnClickedEditWithoutImages(this, new TreeGridBaseEventArgs(GetParentId(treeGrid.SelectedNode), GetChildId(treeGrid.SelectedNode), treeGrid.SelectedNode));
        }
        private void cmiDelete_Click(object sender, EventArgs e)
        {
            OnClickedDelete(this, new TreeGridBaseEventArgs(GetParentId(treeGrid.SelectedNode), GetChildId(treeGrid.SelectedNode), treeGrid.SelectedNode));
        }
        private void cmiAssociateUtility_Click(object sender, EventArgs e)
        {
            OnClickedAssociate(this, new TreeGridBaseEventArgs(GetParentId(treeGrid.SelectedNode), GetChildId(treeGrid.SelectedNode), treeGrid.SelectedNode));
        }
        private void cmiEdit_Click(object sender, EventArgs e)
        {
            OnClickedEdit(this, new TreeGridBaseEventArgs(GetParentId(treeGrid.SelectedNode), GetChildId(treeGrid.SelectedNode), treeGrid.SelectedNode));
        }

        private void cmiCreateAsset_Click(object sender, EventArgs e)
        {
            OnClickedCreateAsset(this, new TreeGridBaseEventArgs(GetParentId(treeGrid.SelectedNode), GetChildId(treeGrid.SelectedNode), treeGrid.SelectedNode));
        }
        private void btiExportToExcel_Click(object sender, EventArgs e)
        {
            OnClickedExportToExcel(this, new EventArgs());
        }
        
        #endregion

        #region Tree
        private void treeGrid_NodeDoubleClick(object sender, DevComponents.AdvTree.TreeNodeMouseEventArgs e)
        {
            if (ActivateDoubleClick())
                OnClickedEdit(this, new TreeGridBaseEventArgs(GetParentId(e.Node), GetChildId(e.Node)));
        }

        private void treeGrid_BeforeNodeDrop(object sender, TreeDragDropEventArgs e)
        {
            try
            {
                if (CancelActionChangeParent(e.Node, e.OldParentNode, e.NewParentNode))
                    e.Cancel = true;
                else
                    BeforeNodeChangeParent(e.Node, e.OldParentNode, e.NewParentNode);

            }catch(Exception ex)
            {
                RegisterError.SaveError(ex);
            }

        }

        #endregion



        

        

        
        

        #endregion

        




    }
}
